package com.dianrui.agent.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.adapter.PlanningRecordAdapter;
import com.dianrui.agent.bean.PlanningRecord;
import com.dianrui.agent.bean.PlanningRecordList;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.dianrui.agent.view.MyListView;
import com.dianrui.agent.view.TrackDatePicker;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PlaningRecordActivity extends BaseActivity {
    private String adminId;
    private int mPageIndex = 1;
    private String chooseDate;
    private List<PlanningRecord> planningRecordAll = new ArrayList<>();
    private List<PlanningRecordList> planningRecordListAll = new ArrayList<>();
    private List<PlanningRecordList> planningRecordListeffecTive = new ArrayList<>();
    private List<PlanningRecordList> planningRecordListInvalid = new ArrayList<>();

    private PlanningRecordAdapter planningRecordAdapter;
    private String type="1";


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.attendance_count)
    TextView attendanceCount;
    @BindView(R.id.month_count)
    TextView monthCount;
    @BindView(R.id.current_date)
    TextView currentDate;
    @BindView(R.id.listview)
    ListView listView;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;
    @BindView(R.id.effective_btn)
    Button effectiveBtn;
    @BindView(R.id.invaild_btn)
    Button invaildBtn;


    @Override
    public int getContentViewId() {
        return R.layout.activity_planningrecord;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                adminId = bundle.getString("adminId");
            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        currentDate.setText(simpleDateFormat.format(date));
        title.setText("调度记录");
        type = "1";
        planningRecordAdapter = new PlanningRecordAdapter(PlaningRecordActivity.this, planningRecordListeffecTive);
        listView.setAdapter(planningRecordAdapter);
        getCharingRecord(chooseDate);


    }

    private void getCharingRecord(String chooseDate) {
        if (planningRecordListeffecTive != null) {
            planningRecordListeffecTive.clear();
        }
        if (planningRecordListInvalid != null) {
            planningRecordListInvalid.clear();
        }
        if (planningRecordListAll != null) {
            planningRecordListAll.clear();
        }
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("manage_id", adminId);
        if (!StringUtils.isEmpty(chooseDate)) {
            jsonObject.addProperty("month_time", chooseDate);
        }
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.DISPATCHRECORED, jsonObject.toString(), new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {

                                attendanceCount.setText(jsonObject.optJSONObject("data").optString("attendance_count"));

                                JSONArray data = jsonObject.optJSONObject("data").optJSONArray("list");
                                if (data.length() > 0) {
                                    for (int i = 0; i < data.length(); i++) {
                                        JSONObject jsonObject1 = data.optJSONObject(i);
                                        PlanningRecordList planningRecordList = new PlanningRecordList();
                                        planningRecordList.date_title = jsonObject1.optString("date_title");
                                        JSONArray jsonArray1 = jsonObject1.optJSONArray("data");
                                        if (jsonArray1.length() > 0) {
                                            List<PlanningRecord> planningRecordLists = new ArrayList<>();
                                            for (int j = 0; j < jsonArray1.length(); j++) {
                                                PlanningRecord planningRecord = new PlanningRecord();
                                                planningRecord.dispatch_id = jsonArray1.optJSONObject(j).optString("dispatch_id");
                                                planningRecord.start_address = jsonArray1.optJSONObject(j).optString("start_address");
                                                planningRecord.start_latitude = jsonArray1.optJSONObject(j).optDouble("start_latitude");
                                                planningRecord.end_time = jsonArray1.optJSONObject(j).optString("end_time");
                                                planningRecord.start_longitude = jsonArray1.optJSONObject(j).optDouble("start_longitude");
                                                planningRecord.end_latitude = jsonArray1.optJSONObject(j).optDouble("end_latitude");
                                                planningRecord.end_longitude = jsonArray1.optJSONObject(j).optDouble("end_longitude");
                                                planningRecord.create_time = jsonArray1.optJSONObject(j).optString("create_time");
                                                planningRecord.number = jsonArray1.optJSONObject(j).optString("number");
                                                planningRecord.is_effect = jsonArray1.optJSONObject(j).optInt("is_effect");
                                                planningRecord.end_address = jsonArray1.optJSONObject(j).optString("end_address");
                                                planningRecordLists.add(planningRecord);
                                            }
                                            planningRecordList.data = planningRecordLists;
                                        }
                                        planningRecordListAll.add(planningRecordList);
                                    }
                                    for (int i = 0; i < planningRecordListAll.size(); i++) {
                                        for (int j = 0; j < planningRecordListAll.get(i).data.size(); j++) {
                                            if (planningRecordListAll.get(i).data.get(j).is_effect==1) {
                                                PlanningRecordList listbean = new PlanningRecordList();
                                                listbean.date_title = planningRecordListAll.get(i).date_title;
                                                listbean.data.add(planningRecordListAll.get(i).data.get(j));
                                                planningRecordListeffecTive.add(listbean);//有效
                                            } else if (planningRecordListAll.get(i).data.get(j).is_effect==2) {
                                                PlanningRecordList listbean = new PlanningRecordList();
                                                listbean.date_title = planningRecordListAll.get(i).date_title;
                                                listbean.data.add(planningRecordListAll.get(i).data.get(j));
                                                planningRecordListInvalid.add(listbean);//无效

                                            }
                                        }

                                    }
                                    if (type.equals("1")) {
                                        if (planningRecordAdapter != null) {
                                            planningRecordAdapter.refresh(planningRecordListeffecTive);
                                        }
                                        monthCount.setText(planningRecordListeffecTive.size()+"");
                                    }else {
                                        if (planningRecordAdapter != null) {
                                            planningRecordAdapter.refresh(planningRecordListInvalid);
                                        }
                                        monthCount.setText(planningRecordListInvalid.size()+"");
                                    }

                                }
                            }else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }else if ("1002".equals(jsonObject.optString("status"))) {
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });

    }

    @SuppressLint("SetTextI18n")
    @OnClick({R.id.back, R.id.calendar_img, R.id.effective_btn, R.id.invaild_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.calendar_img:
                TrackDatePicker datePicker = new TrackDatePicker(PlaningRecordActivity.this);
                datePicker.setDateListener(new TrackDatePicker.OnDateCListener() {

                    @Override
                    public void onDateSelected(String year, String month) {
                        chooseDate = year + "-" + month;
                        currentDate.setText(year + "-" + month);
                       getCharingRecord(chooseDate);
                    }
                });
                datePicker.show();
                break;

            case R.id.effective_btn:
                type="1";
                effectiveBtn.setBackgroundResource(R.drawable.effect_shape);
                invaildBtn.setBackgroundResource(R.drawable.invaild_shape);
                effectiveBtn.setTextColor(getResources().getColor(R.color.white));
                invaildBtn.setTextColor(getResources().getColor(R.color.black));
                effectiveList();
                break;

            case R.id.invaild_btn:
                type="2";
                effectiveBtn.setBackgroundResource(R.drawable.effect_shapes);
                invaildBtn.setBackgroundResource(R.drawable.invaild_shapes);
                effectiveBtn.setTextColor(getResources().getColor(R.color.black));
                invaildBtn.setTextColor(getResources().getColor(R.color.white));
                invaildList();
                break;

            default:
                break;
        }
    }


    private void effectiveList() {
        planningRecordAdapter.refresh(planningRecordListeffecTive);
        monthCount.setText(planningRecordListeffecTive.size() + "");
    }

    private void invaildList() {
        planningRecordAdapter.refresh(planningRecordListInvalid);
        monthCount.setText(planningRecordListInvalid.size() + "");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (planningRecordListAll != null) {
            planningRecordListAll.clear();
            planningRecordListAll = null;
        }
        if (planningRecordAll != null) {
            planningRecordAll.clear();
            planningRecordAll = null;
        }
        if (planningRecordListeffecTive != null) {
            planningRecordListeffecTive.clear();
            planningRecordListeffecTive = null;
        }
        if (planningRecordListInvalid != null) {
            planningRecordListInvalid.clear();
            planningRecordListInvalid = null;
        }
    }
}
